function goAjax(json){
	//alert("ajax提交");
	jQuery.ajax({
		type:"post",
		//localhost
		url:"http://127.0.0.1:8080/webXiangmu/AjaxServer",
		data:JSON.stringify(json),
		dataType:"json",
		success:function(message){
			//判断指令
			//alert("instruct="+message.instruct);
			var instruct = message.instruct;
			if(instruct=="adminLogin"){
				var msg = message.msg;
				if(msg=="false"){
					alert("登录失败，账号或密码错误。");
				}else{
					//var userJson = JSON.parse(msg);
					//将用户信息存入session
					sessionStorage.setItem('adminMsg', msg);
					//获取了msg以后要将msg转换为json对象才能使用
//					var test = sessionStorage.getItem('adminMsg');
//					var adminJson = JSON.parse(test);
//					alert("登录成功，test.uname="+adminJson.uname);
					alert("登录成功！");
					window.location="adminIndex.jsp";
					
				}
			}else if(instruct=="movie_detail"){
				var movie = message.msg;
				//alert(message.msg);
				var body = document.getElementById('middle-left');
				body.innerHTML="<div id=\"vedioandposter\">"+
				"<embed src=\""+movie.trailer+"\" ></embed>"+
				"<img src=\""+movie.picAddress+"\" alt=\"图片找不到了\">"+
			"</div>"+
			"<div id=\"Introduction\">"+
				"<p id=\"movietitle\">"+movie.mname+"</p>"+
				"<p id=\"type\">电影类型："+movie.type+"</p>"+
				"<p id=\"DaoYan\">导演："+movie.daoYan+"</p>"+
				"<p id=\"ZhuYan\">主演："+movie.zhuYan+"</p>"+
				"<p id=\"showtime\">上映时间："+movie.showTime+"</p>"+
				"<p id=\"content\">"+
					"&nbsp;&nbsp;&nbsp;&nbsp;"+movie.mcontent+
				"</p>"+
			"</div>";	
				showMidRight();
			}else if(instruct=="showMovies"){
				var movlistJson = JSON.parse(message.msg);
				//alert(message.msg);
				//alert(movlistJson);
				var tbody = document.getElementById('t1');
				var tbodyHtml = "";
				for(var i in movlistJson){
					tbodyHtml = tbodyHtml+
					"<tr>"+
					"<td class=\"i2\">"+
						"<a href=\"\"><img src=\""+movlistJson[i].picAddress+"\" alt=\"图片找不到了\" class=\"poster\"></a>"+
					"</td>"+
					"<td class=\"message\">"+
						"<p class=\"title\">"+movlistJson[i].mname+"</p>"+
						"<p class=\"type\">类型："+movlistJson[i].type+"</p>"+
						"<p class=\"content\">"+movlistJson[i].mcontent+
						"</p>"+
						"<p><a href=\"/webXiangmu/movie_details.jsp?mid="+movlistJson[i].mid+"\">查看详情>></a></p>"
					"</td>"+
				"</tr>"
				}
				tbody.innerHTML=tbodyHtml;
				loadjscssfile("js/movie.js","js");
				showMidRight();
			}else if(instruct=="addComment"){
				alert("回复成功");
				location.reload();
//				var rid = getUrl('rid');
//				var json = {
//						instruct:"Review_detail",
//						rid:rid
//				}
//				goAjax(json);
			}else if(instruct=="review_detail"){
				var msg = JSON.parse(message.msg);
				var review = msg.review;
				var ridComAl = msg.ridComAl;
				//alert(msg);
				//alert(review.rtitle)
				//alert(ridComAl[1].cId);
				var reviewDiv = document.getElementById('review');
				reviewDiv.innerHTML="<h2>"+review.rtitle+"</h2>"+
				"<p>内容：<br/> 作者："+review.author+"&nbsp;&nbsp;&nbsp;&nbsp;发表时间:"+review.rtime+"<br>&nbsp;&nbsp;"+review.rcontent+"</p>"+
				"<a target=\"_blank\" href=\"review.jsp\">返回</a>"
				var  comments = document.getElementById('comments');
				var commentsMsg = "";
				for(var i in ridComAl){
					commentsMsg = commentsMsg+"<p><strong>"+ridComAl[i].nicname+"&nbsp;&nbsp;"+ridComAl[i].ctime+"</strong><br/>"+ridComAl[i].ccontent+"</p>";
				}
				comments.innerHTML=commentsMsg;
//				var p = document.createElement('p');
//				for(var i in ridComAl){
//					p = document.createElement('p');
//					comments.appendChild(p);
//					p.innerHTML = "<strong>"+ridComAl[i].nicname+"&nbsp;&nbsp;"+ridComAl[i].ctime+"</strong><br/>"+ridComAl[i].ccontent;
//				}
				
				//显示公告
				showMidRight();
				loadjscssfile("js/review_details.js","js");
			}else if(instruct=="showReviews"){//如果是影评页面
				var revlist = message.msg;
				var revlistJson = JSON.parse(revlist);
				//alert("revlist="+revlist);
				//alert(revlistJson);
				var tbody = document.getElementById("reviewTable");
				var tr = document.createElement('tr');
				for(var i in revlistJson){
					tr = document.createElement('tr');
					tbody.appendChild(tr);
					var td1 = document.createElement('td');
					tr.appendChild(td1);
					//给td添加class
					td1.className = "titleAndContent";
					td1.innerHTML="<p class=\"title\">"+revlistJson[i].rtitle+"</p>"+
						"<p class=\"author\">作者："+revlistJson[i].author+"</p>"+
						"<p class=\"content\">&nbsp;&nbsp;&nbsp;&nbsp;"+revlistJson[i].rcontent+"</p>"+
						"<a id=\""+revlistJson[i].rId+"\" class=\"goDetials\" href=\"/webXiangmu/review_details.jsp?rid="+revlistJson[i].rId+"\">阅读全文>></a><span class=\"count\"> 阅读("+revlistJson[i].hits+") | 回复("+revlistJson[i].commentNum+") </span>"
					var td2 = document.createElement('td');
					tr.appendChild(td2);
					td2.className="poster_td";
					td2.innerHTML="<img class=\"poster\" src=\""+revlistJson[i].picAddress+"\">"
				}
				//显示公告
				showMidRight();
				loadjscssfile("js/review.js","js");
			}else if(instruct=="signupReturn"){//如果是返回注册结果
				var msg = message.msg;
				alert("注册结果："+msg);
				if(msg=="注册成功"){
					window.location="review.jsp";
				}
			}else if(instruct=="showPoster"){//如果是海报页面
				var msg = message.msg;
				//alert("msg="+msg);
				var posterListJson = JSON.parse(msg);
				//alert(posterListJson);
//				var poster1 = JSON.parse(posterListJson[1]);
//				alert("poster1.mid="+poster1.mid);
				//显示到页面
				var ul = document.getElementById("posterUl");
				var li = document.createElement('li');
				for(var i in posterListJson){
					var poster = JSON.parse(posterListJson[i]);
					li = document.createElement('li');
					ul.appendChild(li);
					var dl = document.createElement('dl');
					li.appendChild(dl);
					dl.innerHTML = "<dd>"+             //http://localhost:8080/webXiangmu/
                	"<a class=\"book_title\" href=\""+"poster_details.jsp?posterimg="+poster.poster+"&mname="+poster.mname/*海报详情页面*/+"\" value=\"海报图片\"><img src=\""+poster.poster+"\" width=\"140px\" height=\"200px\" />"+
                    	"</a>"+
                    "</dd>"+
                    "<dt>"+                                   //http://localhost:8080/webXiangmu/
                        "<p class=\"book_title\" ><a href=\""+"poster_details.jsp?posterimg="+poster.poster+"&mname="+poster.mname/*海报详情页面*/+"\">"+poster.mname+"</a></p>"+
                        "<p class=\"book_inline\">"+poster.type+"</p>"+
                    "</dt>"
				}
				loadjscssfile("js/poster.js","js");
				//显示公告
				showMidRight();
			}else if(instruct=="userLogin"){//如果是登录
				var msg = message.msg;
				if(msg=="false"){
					alert("登录失败，账号或密码错误。");
				}else{
					//var userJson = JSON.parse(msg);
					//将用户信息存入session
					sessionStorage.setItem('userMsg', msg);
					//获取了msg以后要将msg转换为json对象才能使用
//					var test = sessionStorage.getItem('userMsg');
//					var userJson = JSON.parse(test);
//					alert("登录成功，test.nicname="+userJson.nicname);
					alert("登录成功！");
					checkLogined();
					
				}
			}else if(instruct=="goNotice_details"){//如果是前往公告详情页面
				var noticeJson = JSON.parse(message.msg);
				//alert("nid="+noticeJson.nid);
				$("#noticeTitle").html("<h1>"+noticeJson.ntitle+"</h1>");
				$("#noticeContent").html("&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp"+noticeJson.ncontent);
				//显示公告
				showMidRight();

			}else if(instruct=="showNoticeInMidRight"){//右侧栏显示公告标题
				var noticeListJson = JSON.parse(message.msg);
				var ol = document.getElementById('noticeOl');
				var li = document.createElement('li');
				for(var i in noticeListJson){
					li = document.createElement('li');
					ol.appendChild(li);		//http://localhost:8080/webXiangmu/
					li.innerHTML="<a href=\"notice_details.jsp?nid="+noticeListJson[i].nid+"\">"+noticeListJson[i].ntitle+"</a>";
					if(i>5){
						break;
					}
				}
			}
			else if(instruct=="showNotice"){//如果是显示公告页面
				var noticeListJson = JSON.parse(message.msg);
				//生成列表
				var tbody = document.getElementById('noticeTbody');
				var tr = document.createElement('tr');
				for(var i in noticeListJson){
					tr = document.createElement('tr');
					tbody.appendChild(tr);
					var td = document.createElement('td');
					tr.appendChild(td);
					//生成公告列表，点击调用js方法发送数据
					td.innerHTML = "<a href=\"notice_details.jsp?nid="+noticeListJson[i].nid+"\">"+noticeListJson[i].ntitle+"</a>";
					td = document.createElement('td');
					tr.appendChild(td);
					//alert("ntime"+noticeListJson[i].nid+" = "+noticeListJson[i].ntime.split(" ")[0]);
					var time = noticeListJson[i].ntime.split(" ")[0];
					td.innerHTML = "<p>"+time+"</p>";
				}
				loadjscssfile("js/notice.js","js");
				loadjscssfile("css/notice.css","css");
				//显示公告
				showMidRight();
			}else if(instruct=="votcheck"){//如果是判定投票
				var result = JSON.parse(message.msg);
				if(result!="false"){
					//在指定位置显示票数
					//alert(result.poll);
					$("#poll"+result.mid).html("票数："+result.poll);
				}else{
					alert("投票失败："+result);
				}
			}else if(instruct=="showVote"){//如果是显示投票页面
				var movlist = message.msg;
				//alert("movlist="+movlist);
				//这里获取到的movlist为json字符串，要将movlist转换为json对象
				var movlistJson = JSON.parse(movlist);
				//alert("movlistJson="+movlistJson)
				//生成表格（未完成）
				var tbody = document.getElementById('VoteTable');
				var tr = document.createElement('tr');
				//控制换行
				var listlen = 0;
				for(var i in movlistJson){
					listlen++;
					if(listlen%2==1){
						tr = document.createElement('tr');
						tbody.appendChild(tr);
					}
					var td1 = document.createElement('td');
					tr.appendChild(td1);
					//给td添加class
					td1.className = "moviePhoto";
//					td1.innerHTML = "<embed src=\"https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3372794459,3349200204&fm=26&gp=0.jpg\" class=\"photoEmbed\" width=\"150px\" height=\"225px\"></embed>";
					td1.innerHTML = "<embed src=\""+movlistJson[i].picAddress+"\" class=\"photoEmbed\" width=\"150px\" height=\"225px\"></embed>";
					var td2 = document.createElement('td');
					tr.appendChild(td2);
					td2.className = "VoteContent";
					td2.innerHTML = "<p>"+movlistJson[i].mname+"</p><br>" +
							"<p>"+movlistJson[i].type+"</p><br>" +
							"<p id = \"poll"+movlistJson[i].mid+"\">票数："+movlistJson[i].poll+"</p><br>" +
							"<button id =\"vote,"+movlistJson[i].mid+"\" class=\"voteButton\">投票</button>";
				}
				
				loadjscssfile("js/Vote.js","js");
				loadjscssfile("css/Vote.css","css");
				//显示公告
				showMidRight();
			}else if(instruct == "wrong"){
				alert("服务器内部错误");
				var msg = message.msg;
				if(msg.length!=undefined){
					alert(msg);
				}
			}else{
				alert("没有"+instruct);
			}
			flag = true;
		},
		error:function(message){
			alert("错误");
		}
	});
}

//动态加载js/css
function loadjscssfile(filename, filetype){
	if(filetype == "js"){//判定文件类型
		var fileref = document.createElement('script');//创建标签
		fileref.setAttribute("type","text/javascript");//定义属性type的值为text/javascript
		fileref.setAttribute("src",filename);//文件的地址
	}
	else if (filetype == "css"){//判定文件类型
		var fileref = document.createElement("link");
		fileref.setAttribute("rel","stylesheet");
		fileref.setAttribute("type","text/css");
		fileref.setAttribute("href",filename);
	}
	if(typeof fileref !="undefined")
		document.getElementsByTagName("head")[0].appendChild(fileref);
}

//判断用户是否已登录
function checkLogined(){
	//alert("check!");
	var userMsg = sessionStorage.getItem('userMsg');
	var loginDiv = document.getElementById('login');
	if(userMsg!=undefined){
		//更改登录区块的内容
		var userJson = JSON.parse(userMsg);
		loginDiv.innerHTML="<div><p id=\"wellcometext\">欢迎！"+userJson.nicname+"</p>" +
				"<br><input class=\"btn-info\" id=\"logoutbutton\" type=\"button\" onclick = \"logout()\" value=\"注销\"></input></div>";
	}else{
		loginDiv.innerHTML="<br>"+
				"<p>账&nbsp;号:&nbsp;<input type=\"text\" placeholder=\"账号\" id=\"username\" name=\"username\"/></p>"+
				"<p>密&nbsp;码:&nbsp;<input type=\"password\" placeholder=\"密码\" id=\"password\" name=\"password\" /></p>"+
				"<p>&nbsp;&nbsp;&nbsp;&nbsp;"+
					"<input type=\"button\" class=\"btn-success\" id=\"login\" value=\"登录\" onclick = \"return checkLogin()\"></value>"+
					"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+
					"<input type='button' class=\"btn-info\" value=\"注册\" id=\"signup\" onclick=\"jumptoSignUp()\"></input>"+
					"&nbsp;&nbsp;&nbsp;&nbsp;"+
				"</p>"+
				"<span id=\"msg\" style=\"color:red;\"></span>";
//		loadjscssfile("js/middle_right.js","js");
//		loadjscssfile("js/ajax.js","js");
	}
	loadjscssfile("css/index_style.css","css");
}
//前往注册页面
function jumptoSignUp(){       //localhost
	window.location.href='http://127.0.0.1:8080/webXiangmu/signup.jsp';
}
//注销
function logout(){
	//sessionStorage.clear();
	sessionStorage.removeItem('userMsg');
	checkLogined();
	alert("注销成功");
}
//显示公告
function showMidRight(){
	var json2 = {
			instruct:"takeNoticeMsgInMidRight"
		}
	goAjax(json2);
}
